package com.tools.web.interceptor.limit;

import com.tools.common.object.Note;

/**
 * IP-URI 请求限流拦截器的 Redis-Lua 脚本
 * */
@Note("IP-URI 请求限流拦截器的 Redis-Lua 脚本")
public interface RequestLimitRedisLuaScript {

    String CHECK_LIMIT =
            "local value = redis.call('GET', KEYS[1])" +
            "if not value then" +
            "   redis.call('SETEX', KEYS[1], ARGV[1], 1)" +
            "   return 1" +
            "end" +
            "local requestCount = tonumber(value)" +
            "local allow = tonumber(ARGV[2])" +
            "if requestCount >= allow then" +
            "   return 0" +
            "end" +
            "local ttl = redis.call('TTL', KEYS[1])" +
            "redis.call('SETEX', KEYS[1], ttl, requestCount + 1)" +
            "return 1";
}
